<?php

class Validator
{
    protected static $errors = array();
    
    protected static function saveError($err)
    {
        self::$errors[] = $err;
    }
    
    public function getErrors()
    {
        return self::$errors;
    }
    
    public static function isNotEmpty($var, $saveError = true)
    {
        if (empty($var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    public static function isNum($var, $saveError = true)
    {
        if (!is_numeric($var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    
    public static function isFloat($var, $saveError = true)
    {
        if (!is_float($var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    
    public static function isInt($var, $saveError = true)
    {
        if (!is_int($var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    
    public static function isEmail($var, $saveError = true)
    {
        if (!preg_match('/^[a-zA-Z0-9]+[\.a-zA-Z0-9_-]*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-])+/', $var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    
    public static function isName($var, $saveError = true)
    {
        if (!preg_match('/^[a-zA-Z0-9]{4,12}/', $var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
    
    public static function isURL($var, $saveError = true)
    {
        if (!preg_match("(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?", $var)) {
            if ($saveError) self::saveError(__METHOD__.':'.$var);
            return false;
        }
        return true;
    }
}